<?php

class Manage_Model extends Model {

    function Manage_Model()
    {
        // Call the Model constructor
        parent::Model();
    }
    
    function get_entries()
    {
		$this->load->database();
		//main menu
		$query = $this->db->query('SELECT * FROM gcms_posts WHERE post_menu="1" ORDER BY post_order ASC');
		echo '<div class="option_box"><h2>Pages in Main Menu</h2>';
		echo '<p style="padding-top:0px;"><ul style="list-style-image:url('.base_url().'system/images/page.png); min-height:10px;" id="menu_list">';
		foreach ($query->result() as $row)
		{
		    $title = $row->post_title;
			$id = $row->ID;
			echo '<li id="menu_list_'.$id.'">';
			echo '<a href="manage/edit/'.$id.'">'.$title.'</a> (<a href="'.base_url().$row->post_slug.'" target="_top">View</a>)';
			echo '</li>';
		}
		echo '</ul><br /></p></div>';
		//other pages
		$query = $this->db->query('SELECT * FROM gcms_posts WHERE post_menu="0" ORDER BY post_title ASC');
		echo '<div class="option_box"><h2>Other Pages</h2>';
		echo '<p style="padding-top:0px;"><ul style="list-style-image:url('.base_url().'system/images/page.png); min-height:10px;" id="other_list">';
		foreach ($query->result() as $row)
		{
		    $title = $row->post_title;
			$id = $row->ID;
			echo '<li id="other_list_'.$id.'">';
			echo '<a href="manage/edit/'.$id.'">'.$title.'</a> (<a href="'.base_url().$row->post_slug.' target="_top">View</a>)';
			echo '</li>';
		}
		echo '</ul></p><br /></div>';
    }

    function insert_entry()
    {
		$this->load->database();
		$this->load->helper('url');
		
        $title = $_POST['title'];
		$content = $_POST['content'];
		$date = date('Y-m-d H:i:s'); 
		$author = "Gilbitron";
		$slug = url_title($_POST['title']);
		$order = 99;
		$menu = 0;
		
		$data = array(
               'post_author' => $author,
               'post_date' => $date,
			   'post_content' => $content,
			   'post_title' => $title,
			   'post_slug' => $slug,
			   'post_order' => $order,
			   'post_menu' => $menu
            );

		$this->db->insert('gcms_posts', $data); 
    }

    function update_entry()
    {
		$this->load->database();
        $title = $_POST['title'];
		$content = $_POST['content'];
		$slug = url_title($_POST['slug']);
		
		$data = array(
			   'post_content' => $content,
			   'post_title' => $title,
			   'post_slug' => $slug
            );

		$this->db->where('ID', $this->uri->segment(3));
		$this->db->update('gcms_posts', $data); 
    }
	
	function delete_entry($id)
	{
		$this->load->database();
		$this->db->where('ID', $id);
		$this->db->delete('gcms_posts'); 
	}

}

?>